package com.wika.basics.widget

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import androidx.appcompat.widget.AppCompatButton
import androidx.core.content.ContextCompat
import com.wika.basics.R


/**
 * @Description: 自定义点击效果Button
 * @Date: 2024/1/15 11:38
 * @Author: WangWeiShuo
 */
class ClickButton @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
): AppCompatButton(context,  attrs, defStyleAttr) {

    private var mNormalDrawable: Drawable? = null
    private var mNormalColor = 0
    private var mNormalTextColor = 0

    private var mClickDrawable: Drawable? = null
    private var mClickColor = 0
    private var mClickTextColor = 0

    private var hasPressed = false

    private val BUTTON_MODE_CLICK = 0
    private val BUTTON_MODE_SELECT = 1
    private val BUTTON_MODE_LONG_CLICK_SELECT = 2
    private val BUTTON_MODE_SWITCH = 3
    private var mButtonMode = BUTTON_MODE_CLICK

    private val BACKGROUND_MODE_PIC = 0
    private val BACKGROUND_MODE_COLOR = 1
    private val BACKGROUND_MODE_XML = 2
    private val BACKGROUND_MODE_DEFAULT = 3
    private var mBackgroundMode = BACKGROUND_MODE_DEFAULT

    private var buttonClickListener: OnButtonClickListener? = null

    init {
        initAttrs(context,attrs)
    }

    private fun initAttrs(context: Context, attrs: AttributeSet?) {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.ClickButton)

        // 背景
        val normalDrawable = typedArray.getDrawable(R.styleable.ClickButton_normalColor)
        mNormalTextColor = typedArray.getColor(R.styleable.ClickButton_textColor,
            ContextCompat.getColor(context, R.color.black))
        mClickTextColor = typedArray.getColor(R.styleable.ClickButton_textPressedColor,
            ContextCompat.getColor(context, R.color.black))
        mButtonMode = typedArray.getInt(R.styleable.ClickButton_buttonMode,0)

        if (null != normalDrawable) {
            when (normalDrawable) {
                is GradientDrawable -> { // XML
                    mNormalDrawable = normalDrawable
                    mBackgroundMode = BACKGROUND_MODE_XML
                    background = mNormalDrawable
                }
                is ColorDrawable -> { // 色值
                    mNormalColor = (normalDrawable as ColorDrawable).color
                    mBackgroundMode = BACKGROUND_MODE_COLOR
                    setBackgroundColor(mNormalColor)
                }
                else -> { // 其他形式
                    mNormalDrawable = normalDrawable
                    mBackgroundMode = BACKGROUND_MODE_PIC
                    background = mNormalDrawable
                }
            }
        }

        //点击背景
        val clickDrawable = typedArray.getDrawable(R.styleable.ClickButton_pressedColor)
        if (null != clickDrawable) {
            when (clickDrawable) {
                is GradientDrawable -> { // XML
                    mClickDrawable = clickDrawable
                    mBackgroundMode = BACKGROUND_MODE_XML
                }
                is ColorDrawable -> { // 色值
                    mClickColor = (clickDrawable as ColorDrawable).color
                    mBackgroundMode = BACKGROUND_MODE_COLOR
                }
                else -> { // 设置了一张图片
                    mClickDrawable = clickDrawable
                    mBackgroundMode = BACKGROUND_MODE_PIC
                }
            }
        }
        setTextColor(mNormalTextColor)
        setOnClickListener {
        }
        setOnLongClickListener {
            if(mButtonMode == BUTTON_MODE_LONG_CLICK_SELECT){
                if(hasPressed){
                    hasPressed = false
                    setButtonNormal(true)
                }else {
                    hasPressed = true
                    setButtonStyle(true)
                }
            }
            true
        }

//        val drawable = GradientDrawable()
//        drawable.cornerRadius = 10f
//        background = drawable
        typedArray.recycle()
    }

    override fun setOnLongClickListener(l: OnLongClickListener?) {
        super.setOnLongClickListener(l)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> {
                if(mButtonMode == BUTTON_MODE_SELECT){
                    if(hasPressed){
                        hasPressed = false
                        setButtonNormal(true)
                    }else {
                        hasPressed = true
                        setButtonStyle(true)
                    }
                }else if(mButtonMode == BUTTON_MODE_CLICK){
                    setButtonStyle(true)
                }else if(mButtonMode == BUTTON_MODE_SWITCH){
                    setButtonStyle(true)
                }
            }

            MotionEvent.ACTION_UP -> {
                if(mButtonMode != BUTTON_MODE_SWITCH) {
                    setButtonStyle(false)
                }
            }
        }
        return super.onTouchEvent(event)
    }

    /**
     * 设置Button背景
     * @param down Boolean true：按下，false：抬起
     */
    private fun setButtonStyle(down: Boolean){
        if(down){
            setButtonPress()
            if(buttonClickListener != null){
                buttonClickListener?.onDown()
            }
        }else{
            if(mButtonMode == BUTTON_MODE_CLICK) {
                setButtonNormal(true)
            }
        }
    }

    private fun setButtonNormal(callBack:Boolean){
        when (mBackgroundMode) {
            BACKGROUND_MODE_PIC -> {
                background = mNormalDrawable
            }

            BACKGROUND_MODE_COLOR -> {
                setBackgroundColor(mNormalColor)
            }

            BACKGROUND_MODE_XML -> {
                background = mNormalDrawable
            }
        }
        setTextColor(mNormalTextColor)
        if(callBack){
            if(buttonClickListener != null){
                buttonClickListener?.onLift()
            }
        }
    }

    fun setButtonPress(){
        when (mBackgroundMode) {
            BACKGROUND_MODE_PIC -> {
                background = mClickDrawable
            }

            BACKGROUND_MODE_COLOR -> {
                setBackgroundColor(mClickColor)
            }

            BACKGROUND_MODE_XML -> {
                background = mClickDrawable
            }
        }
        setTextColor(mClickTextColor)
    }

    fun resetButton(){
        setButtonNormal(false)
    }

    fun setOnButtonClickListener(listener: OnButtonClickListener){
        buttonClickListener = listener
    }

    interface OnButtonClickListener{
        fun onDown()//按下
        fun onLift()//抬起
    }
}